﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ERModelData
{
    /// <summary>Сущность</summary>
    class Essence : Border
    {
        /// <summary>Контрол отражающий имя сущности</summary>
        public Label name { get; set; }
        
        /// <summary>Панель содержащая атрибуты</summary>
        public StackPanel atributs { get; set; }

        public LinkMarkers links { get; private set; }

        /// <summary>Констуктор класса сущность</summary>
        /// <param name="location">Верхний левый угол сущности</param>
        /// <param name="size">Размер сущности</param>
        public Essence(Point location, Size size, Canvas parent)
        {
            /// Настройка контрола отображения имени сущности
            name = new Label();          
            name.Content = "Essence1";
            name.MouseDoubleClick += new MouseButtonEventHandler(ChangeName);

            /// Настройка панели содержащая атрибуты сущности
            atributs = new StackPanel();
            atributs.Orientation = Orientation.Vertical;

            /// Настройка контрола для отображения атрибутов
            Expander atributsExpander = new Expander();
            atributsExpander.Header = "Атрибуты";
            atributsExpander.BorderBrush = Brushes.Black;
            atributsExpander.BorderThickness = new Thickness(0, 1, 0, 0);
            atributsExpander.Content = atributs;
            
            /// Настройка панели внутри сущности
            StackPanel inBorder = new StackPanel();
            inBorder.Orientation = Orientation.Vertical;
            name.Tag = inBorder.Children.Add(name);
            inBorder.Children.Add(atributsExpander);

            /// Контекстное меню
            ContextMenu EssenceMenu = new ContextMenu();

            Label addAtribute = new Label();
            addAtribute.Content = "Добавить атрибут";
            addAtribute.MouseLeftButtonUp += new MouseButtonEventHandler(AddAtribute);
            EssenceMenu.Items.Add(addAtribute);

            Label delEssence = new Label();
            delEssence.Content = "Удалить сущность";
            delEssence.MouseLeftButtonUp += new MouseButtonEventHandler(DelEssence);
            EssenceMenu.Items.Add(delEssence);

            this.ContextMenu = EssenceMenu;

            /// Настройка Сущности
            this.Background = Brushes.LightBlue;
            this.BorderBrush = Brushes.DarkBlue;
            this.BorderThickness = new Thickness(2);
            this.Width = size.Width;
            this.Height = size.Height;
            Canvas.SetLeft(this, location.X);
            Canvas.SetTop(this, location.Y);
            this.Child = inBorder;
            parent.Children.Add(this);
            links = new LinkMarkers(this);
        }

        /// <summary>Создание атрибута сущности</summary>
        void AddAtribute(object sender, MouseButtonEventArgs e)
        {
            Label name = new Label();
            name.Content = "atribut1";
            name.Name = "name";
            name.Tag = 1;
            name.MouseDoubleClick += new MouseButtonEventHandler(ChangeName);

            Image key = new Image();
            key.Source = new BitmapImage(new Uri(@"Icons/key.png", UriKind.Relative));
            key.Stretch = Stretch.Uniform;
            key.Width = 10;
            key.Height = 15;

            StackPanel panel = new StackPanel();
            panel.Orientation = Orientation.Horizontal;
            panel.Children.Add(key);
            panel.Children.Add(name);

            atributs.Children.Add(panel);
        }

        /// <summary>Удаление сущности</summary>
        void DelEssence(object sender, MouseButtonEventArgs e)
        {
            Canvas parent = (Canvas)this.Parent;
            parent.Children.Remove(this);
            parent.transform.Hiden();
        }

        ///////////////////////////////////////////////////////////////////////////////////
        //////                  Изменение имени сущности или атрибута               ///////
        ///////////////////////////////////////////////////////////////////////////////////

        /// <summary>Контрол для изменения имени сущности атрибута</summary>
        TextBox text = new TextBox();

        /// <summary>Начало редактирования</summary>
        void ChangeName(object sender, RoutedEventArgs e)
        {
            text.LostFocus += new RoutedEventHandler(EndChangeName);
            text.Tag = sender;
            Label label = (Label)sender;
            StackPanel parent = (StackPanel)label.Parent;
            ((Canvas)this.Parent).transform.IgnoreCall = true;
            ((Canvas)this.Parent).transform.Hiden();
            text.Text = label.Content.ToString();
            parent.Children.RemoveAt((int)label.Tag);
            parent.Children.Insert((int)label.Tag, text);
        }
        
        /// <summary>Завершение редактирования</summary>
        void EndChangeName(object sender, RoutedEventArgs e)
        {
            text.LostFocus -= EndChangeName;
            StackPanel parent = (StackPanel)text.Parent;
            Label label = (Label)text.Tag;
            label.Content = text.Text;
            ((Canvas)this.Parent).transform.IgnoreCall = false;
            ((Canvas)this.Parent).transform.Show(this);
            parent.Children.RemoveAt((int)label.Tag);
            parent.Children.Insert((int)label.Tag, label);
        }
    }
}
